<?php

/**
*	A general report on the accessibility of a web page. This is the page
*	callback for node/%node/webpage-accessibility
*/

function quail_server_accessibility_tab_form() {
	
  module_load_include('inc', 'quail_server');
  drupal_add_css(drupal_get_path('module', 'accessible_content') .'/accessible_content.css');
  $node = menu_get_object();
  $website = node_load($node->website);
  $result = drupal_http_request($node->original_url);
  $content = _quail_server_get_content_from_template($result->data, $website, $node->file_path);
  $cms_mode  = ($content != $result->data);
  $results = quail_server_check_string($content, $node->original_url, $website->ac_server['guideline_nid'], $cms_mode);
  if($node->content_hash != md5($content)) {
	$node->total_nodes = _quail_server_count_nodes($content);
	$node->search_text = strip_tags($content);
	$node->accessibility_results = array('content' => $results);
	$node->content_hash = md5($content);
	$node->revision = TRUE;
	node_save($node);
  }
  $form = accessible_content_accessibility_report($results, null, 'Web Page');
  
  drupal_set_breadcrumb(
	array(
	l('Home', '<front>'),
	l('Websites', 'websites'),
	l($website->title, 'node/'. $website->nid),
	l($node->title, 'node/'. $node->nid),
  ));
  
  return $form;
}

/**
*	Returns a browser for all the templates which apply to a node
*/
function quail_server_template_browser($node) {
	$templates = db_query('SELECT nid FROM {quail_server_template} WHERE website_nid = %d ORDER BY default_template', $node->nid);
	$header = array('Name', 'XPath', 'URL Pattern');
	$table = array();
	while($template_nid = db_result($templates)) {
		$template = node_load($template_nid);
		$row_note = ($template->ac_template['default_template'])
					 ? ' <span class="default-template">(Default)</span>'
					 : '';
		$table[] = array(l($template->title, 'node/'. $template->nid) . $row_note, 
						$template->ac_template['xpath_query'],
						$template->ac_template['url_path']);
	}
	
	return l('Add Template', 'node/add/ac-template', array('query' => array('website' => $node->nid))) . theme('table', $header, $table);	
}

/**
*	Renders out a file browser for all the webpages which apply to a website
*/
function quail_server_file_browser($node, $basepath = false) {
	module_load_include('inc', 'quail_server');
	$linkpath = ($basepath)
				? $basepath
				: '/';
	
	$header = array('Type', 'Name', 'Severe', 'Moderate', 'Suggestion');
	$files = array();
	$table = array();
	if($linkpath == '/') {
		$url = parse_url($node->ac_server['start_uri']);
		$files[] = array('type' => 'domain', 'title' => $url['host']);
		if($node->ac_server['span_hosts']) {
			foreach(explode("\n", $node->ac_server['domains']) as $domain) {
				$files[] = array('type' => 'domain', 'title' => $domain);
			}
		}
	}
	else {
		$base = explode('/', $linkpath);
		$result = db_query('SELECT DISTINCT nid, url, file_path FROM {quail_server_webpage} WHERE url LIKE "%%//%s/%%" and website_nid = %d', trim($linkpath, '/'), $node->nid);
		while($row = db_fetch_object($result)) {
			$path = explode('/', $row->file_path);
			$path[0] = $base[0];
			$finalpath = '';
			foreach($path as $k => $item) {
				$finalpath .= $item .'/';
				if(!$base[$k]) {
					if($path[$k+1]) {
						$files[$finalpath] = array('type' => 'folder', 'title' => $item, 'path' => $finalpath);
					}
					else {
						$page_node = node_load($row->nid);
						$files[$item] = array('type' => 'page', 'title' => $item, 'node' => $page_node);
					}
					break;
				}
			}
		}
	}
	foreach($files as $file) {
		if($file['type'] == 'domain') {
			$table[] = array(
				theme('image', drupal_get_path('module', 'quail_server') .'/images/domain.png', 'Domain'),
				l($file['title'], 'node/'. $node->nid, array('query' => array('browse' => $file['title']))),
				'',
				'',
				'',
			);
		}
		if($file['type'] == 'folder') {
			$table[] = array(
				theme('image', drupal_get_path('module', 'quail_server') .'/images/folder.png', 'Directory'),

				l($file['title'], 'node/'. $node->nid, array('query' => array('browse' => $file['path']))),
				'',
				'',
				'',
			);
		}
		elseif($file['type'] == 'page') {
			$row = array();
			$row[] = 
				theme('image', drupal_get_path('module', 'quail_server') .'/images/page.png', 'Page');
			if(!trim($file['title'])) {
				$file['title'] = 'index';
			}
			if($file['node']->nid) {
				$row[] = l($file['title'], 'node/'. $file['node']->nid) . '<div class="filename-label">'. $file['node']->title .'</div>';
				$row[] = $file['node']->accessibility_totals['severe'];
				$row[] = $file['node']->accessibility_totals['moderate'];
				$row[] = $file['node']->accessibility_totals['suggestion'];
				
			}
			else {
				$row[] =  $file;
				$row[] = array('data' => 'Not Collected', 'colspan' => 3);
			}
			$table[] = $row;
		}
	}
	$output = '<p>'. l('Home', 'node/'. $node->nid, array('attributes' => array('class' => 'browse-file-path'))) .' / ';
	$path = explode('/', trim($linkpath, '/'));
	$links = array();
	foreach($path as $p) {
		$start_path .= $p .'/';
		$links[] = l($p, 'node/'. $node->nid, 
					array('attributes' => array('class' => 'browse-file-path'),
					'query' => array('browse' => $start_path)));
	}
	$output .= implode(' / ', $links).'</p>';
	$output .= theme('table', $header, $table);
	return $output;
}

/**
*	Renders a graph of accessibility problems over time for a specific website node
*/
function quail_server_website_graph($node) {
	$history = db_query('select a.*, r.timestamp FROM accessible_content_node_totals a
LEFT JOIN node_revisions r ON r.vid = a.vid
Where a.nid IN (SELECT nid FROM quail_server_webpage WHERE website_nid = %d)', $node->nid);

  while($row = db_fetch_object($history)) {
     $data['severe'][date('y-m-d', $row->timestamp)] += $row->severe_total;
     $data['moderate'][date('y-m-d', $row->timestamp)] += $row->moderate_total;
     $data['suggestion'][date('y-m-d', $row->timestamp)] += $row->suggestion_total;
  }
  $result = array();
  foreach($data as $d) {
  	foreach($d as $k => $v) {
  		foreach($v as $d => $t) {
  			$result[$k][] = array((1000 * strtotime($d)), $t);
  		}
  	}
  }
  return _quail_server_history_graph($result);
}

/**
*	Renders a chart of problems over time for the current webpage
*/
function quail_server_webpage_history($node) {
	$history = db_query('select a.*, r.timestamp FROM accessible_content_node_totals a
LEFT JOIN node_revisions r ON r.vid = a.vid
Where a.nid = %d', $node->nid);
  $data = array();
  while($row = db_fetch_object($history)) {
     $data['severe'][] = array((1000 * $row->timestamp), $row->severe_total);
     $data['moderate'][] = array((1000 * $row->timestamp), $row->moderate_total);
     $data['suggestion'][] = array((1000 * $row->timestamp), $row->suggestion_total);
  }
  return _quail_server_history_graph($data);
}

function _quail_server_history_graph($data) {
  if(!module_exists('flot')) {
  	return null;
  }
  drupal_add_js(drupal_get_path('module', 'quail_server') .'/quail_server.flot.js');
  $result = array(array('label' => 'Severe',
  						'data' => $data['severe']),
  				  array('label' => 'Moderate',
  						'data' => $data['moderate']),
  				  array('label' => 'Suggestion',
  						'data' => $data['suggestion']),
 				);		

  return theme_flot_graph(array('id' => 'flot-graph', 'style' => "width:400px;height:300px"), $result, array(
						'colors' => array('#f72034', '#00608a', '#9ed538'),
						'legend' => array('container' => '#graph-legend'),
						'series' => array('points' => array('show' => 1),
										  'lines' => array('show' => 1)),
						'xaxis' => array('mode' => 'time',
										 'timeformat' => "%y/%m/%d",
										 'ticks' => 5),
						'grid' => array('hoverable' => 1))) .
						'<div id="graph-legend"></div>';

}
/**
*	Renders an overview block of a website
*/
function quail_server_website_overview($node) {
	$totals = _quail_server_get_website_totals($node->nid);
	$output  = '<div class="accessibility-score"><h5>Current Accessibility Score:</h5> <div class="score">'.
	$output .= quail_server_get_page_score(null, $totals['severe'], $totals['moderate'], $totals['nodes']);
	$output .= '</div></div>';
	$output .= '<div id="website-totals">';
	$output .= '<h4>Totals:</h4>';
	$output .= '<div class="total severe"><span class="label">';
	$output .= theme('image', variable_get('ac_ui_icon_severe', drupal_get_path('module', 'accessible_content') .'/images/test-demo-severe.png'), 'Severe');
	$output .= 'Severe:</span> '. $totals['severe'] .'</div>';
	
	$output .= '<div class="total moderate"><span class="label">';
	$output .= theme('image', variable_get('ac_ui_icon_moderate', drupal_get_path('module', 'accessible_content') .'/images/test-demo-moderate.png'), 'Moderate');
	$output .= 'Moderate:</span> '. $totals['moderate'] .'</div>';
	
	$output .= '<div class="total suggestion"><span class="label">';
	$output .= theme('image', variable_get('ac_ui_icon_suggestion', drupal_get_path('module', 'accessible_content') .'/images/test-demo-suggestion.png'), 'Severe');
	$output .= 'Suggestion:</span> '. $totals['suggestion'] .'</div>';

	$output .= '<div class="total">';
	$output .= theme('image', drupal_get_path('module', 'quail_server') .'/images/page.png', '');
	$output .= '<span class="label">Pages: </span>'. $totals['pages'] .'</div>';
	$output .= '</div>';
	return $output;
}

/**
*	
*/
function quail_server_highlight_page($node) {
  $website = node_load($node->website);
	drupal_add_css(drupal_get_path('module', 'quail_server') .'/quail_server.css');
	$output = '<p>'. l('View in new window', 'node/'. $node->nid .'/webpage-accessibility/highlight-frame/'. arg(4),
				array('attributes' => array('target' => '_blank', 'class' => 'iframe-pop-out'))) .'</p>';
	$output .= '<iframe class="preview-iframe" src="'. base_path() .'node/'. $node->nid .'/webpage-accessibility/highlight-frame/'. arg(4) .
				'" title="A highlighted view of the accessibility problems with this page. You may use the link above to view this content">Your browser doesn not support frames. You may '. 
				l('open this content in a new window', 
					'node/'. $node->nid .'/webpage-accessibility/highlight-frame/'. arg(4)) .
				'.</iframe>';
  drupal_set_breadcrumb(
	array(
	l('Home', '<front>'),
	l('Websites', 'websites'),
	l($website->title, 'node/'. $website->nid),
	l($node->title, 'node/'. $node->nid),
  ));

	return $output;
}

/**
*	Renders a highlighted view of a page.
*/
function quail_server_highlight_page_frame($node, $test_nid = null) {
      module_load_include('inc', 'quail_server');
	  global $base_url;
	  $website = node_load($node->website);
      $htmlclass = new quailServerRenderHTML();
	  accessible_content_include_library();
	  $result = drupal_http_request($node->original_url);
	  $display_level = array(
      	QUAIL_TEST_SEVERE  => TRUE,
      	QUAIL_TEST_MODERATE => TRUE,
      	QUAIL_TEST_SUGGESTION => TRUE,
	  );
	  if(!$result) {
	  	print '<h2>Could not access address.</h2>';
	  	exit();
	  }
	  
	  $text = _quail_server_get_content_from_template($result->data, $website, $node->original_url, FALSE);
  	  $cms_mode = ($text != $result->data);
	  $text = str_replace('&nbsp;', ' ', $text);
	  $text = $htmlclass->completeURLs($text, $node->original_url);
	  $quail = new quail($text, 'accessible_content', 'string', 'AccessibleContentHighlight');
	  $quail->setUri($node->original_url);
	  $quail->setOption('cms_mode', $cms_mode);
	  if($test_nid) {
	    $test = node_load($test_nid);
	    $quail->runCheck(array('ac_module_test' => $test, 'display_level' => $display_level));
	    
	  }
	  else {
	  	$website = node_load($node->website);
	    $guideline = node_load($website->ac_server['guideline_nid']);
	    $quail->runCheck(array('ac_module_guideline' => $guideline, 'display_level' => $display_level));
	  }
	  $html = $quail->getReport(array('image_url' => array(
          QUAIL_TEST_SEVERE => $base_url .'/'.
            variable_get('ac_ui_icon_severe',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-severe.png'),
          QUAIL_TEST_MODERATE => $base_url .'/'.
            variable_get('ac_ui_icon_moderate',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-moderate.png'),
          QUAIL_TEST_SUGGESTION => $base_url .'/'.
            variable_get('ac_ui_icon_suggestion',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-suggestion.png'),
              ),
              'highlight_classes' => array(
                QUAIL_TEST_SEVERE => variable_get('ac_ui_class_severe', 'accessibility-error-severe'),
                QUAIL_TEST_MODERATE => variable_get('ac_ui_class_moderate', 'accessibility-error-moderate'),
                QUAIL_TEST_SUGGESTION => variable_get('ac_ui_class_severe', 'accessibility-error-suggestion'),
              ),
       ));
       if($cms_mode) {
       	  $html = '<html><head><title>'. $node->title.
		   '</title></head><body>'. $html .'</body></html>';
	   }
       
       print _quail_server_add_css_to_highlight($html);
	exit();
}

function quail_server_template_tab($node) {
	    $website = node_load($node->website);
	    drupal_set_breadcrumb(
        	array(
        	l('Home', '<front>'),
        	l('Websites', 'websites'),
        	l($website->title, 'node/'. $website->nid),
        	l('Templates', 'node/'. $website->nid),
        	l($node->title, 'node/'. $node->nid),
        ));
        $output = '<p>'. l('View in new window', 'node/'. $node->nid .'/template/select',
				array('attributes' => array('target' => '_blank', 'class' => 'iframe-pop-out'))) .'</p>';
	return $output;
}

/**
*	Renders out a page with appended javascript so we can select
*	the xpath for a template node.
*	@param object $node The node that this template is going to be built on
*/
function quail_server_template_select($node) {
	global $base_root;
	module_load_include('inc', 'quail_server');
	$scripts = array('misc/jquery.js',
					 'misc/drupal.js',
					 drupal_get_path('module', 'quail_server') .'/quail_server.js',);
	$result = drupal_http_request($node->original_url);
	if(!$result) {
		return 'Page unavailable';
	}
	$htmlclass = new quailServerRenderHTML();
	$base = $base_root . base_path();
	$text = str_replace('&nbsp;', ' ', $result->data);
	$dom = new DOMDocument();
	$dom->loadHTML($text);
	$head = $dom->getElementsByTagName('head');
	$head = $head->item(0);
	foreach($scripts as $script) {
		$javascript = $dom->createElement('script');
		$javascript = $head->appendChild($javascript);
		$javascript->setAttribute('src', $base . $script);
		$javascript->setAttribute('type', 'text/JavaScript');
	}
	$style = $dom->createElement('style');
	$style->setAttribute('type', 'text/css');
	$style->nodeValue = '.ac_template_selected { border: 2px dashed red; } #ac-toolbar { position: fixed; bottom: 0px; background: black; color: white; font-weight: bold; width: 100%; }';
	$head->appendChild($style);
	$html = $htmlclass->completeURLs($dom->saveHTML(), $node->original_url);
	$toolbar = '<div id="ac-toolbar"><form method="post" action="'. $base .'template-redirect"><input type="submit" value="Select Element"><input type="hidden" name="webpage" value="'. $node->nid .'"><input type="hidden" name="template_name" value="'. $node->title .'"><input type="hidden" name="website_nid" value="'. $node->website .'"><input id="ac-form-xpath" name="xpath_query" value="" type="hidden"><span id="ac-form-xpath-preview"></span></form></div>';
	print str_replace('</body>', $toolbar .'</body>', $html);
	die();
}

/**
*	Once a user builds an xpath for a template, they
*	are redirected to this page callback to build the node. Once the template
*	node is built, they are then redirected to edit the just-created template.
*/	
function quail_server_template_redirect() {
	module_load_include('inc', 'quail_server');
	$webpage = node_load($_POST['webpage']);
	$xpath = _quail_server_cleanup_xpath($_POST['xpath_query']);
	$dom = new DOMDocument();
	$dom->loadHTML(file_get_contents($webpage->original_url));
	$query = new DOMXPath($dom);
	$content = $query->query($xpath);
	if($content->length == 0 && strpos($xpath, 'tbody') !== FALSE) {
		$xpath = str_replace('tbody/', '', $xpath);
	}
	$node = new stdClass();
	$node->title = 'Template For '. $_POST['template_name'];
	$node->type = 'ac_template';
	$node->xpath_query = $xpath;
	$node->website_nid = $_POST['website_nid'];
	$node->url_path = '';
	node_save($node);
	drupal_goto('node/'. $node->nid .'/edit');
}


/**
*	Page callback to preview what the page
*	will look like with the template removed.
*	We also append a floating div just to make
*	sure folks can get back to the page
*/
function quail_server_template_preview_tab($node) {
	module_load_include('inc', 'quail_server');
	$htmlclass = new quailServerRenderHTML();
	$website = node_load($node->website);
	$content = drupal_http_request($node->original_url);
	if($content) {
		$html = _quail_server_get_content_from_template($content->data, $website, $node->original_url);
		$html = $htmlclass->completeURLs($html, $node->original_url);
		$toolbar = '<div style="position: fixed; border: 2px solid black; padding: 4px; bottom: 0px;">'. l('Return to web page', 'node/'. $node->nid) .'</div>';
		print $html . $toolbar;

		die();
	}
}

/**
*	Page callback to check a URL. 
*	@param int $guideline_nid The guideline node ID to load
*/
function quail_server_quick_check($guideline_nid) {
	global $base_url;
	accessible_content_include_library();
	module_load_include('inc', 'quail_server');
	drupal_add_css(drupal_get_path('module', 'quail_server') .'/quail_server.css');
	$guideline = node_load($guideline_nid);
	$page = drupal_http_request($_GET['url']);
	if($page->data && $_GET['mode'] == 'highlight-frame') {
	  $quail = new quail($page->data, 'accessible_content', 'string', 'AccessibleContentHighlight');
	  $quail->setUri($_GET['url']);
	  $quail->runCheck(array('ac_module_guideline' => $guideline));
	  print $quail->getReport(array('image_url' => array(
	          QUAIL_TEST_SEVERE => $base_url .'/'. 
	            drupal_get_path('module', 'accessible_content') .'/images/test-demo-severe.png',
	          QUAIL_TEST_MODERATE => $base_url .'/'. 
	            drupal_get_path('module', 'accessible_content') .'/images/test-demo-moderate.png',
	          QUAIL_TEST_SUGGESTION => $base_url .'/'. 
	            drupal_get_path('module', 'accessible_content') .'/images/test-demo-suggestion.png')));
      exit();
	}
	if($page->data && !$_GET['mode']) {
	  $results = quail_server_check_string($page->data, $_GET['url'], $guideline->nid, false);
      return '<p>'. l('Highlighted View', 'quick-check/'. $guideline->nid,
					array('query' => array('url' => $_GET['url'], 'mode' => 'highlight'), 
						   'attributes' => array('class' => 'highlight-view'))) .'</p>'.
					drupal_render_form('accessibility_results', 
					accessible_content_accessibility_report($results, null, 'Web Page', null, false));
	}
	if($page->data && $_GET['mode'] == 'highlight') {

		$output = '<p>'. l('View in new window', 'quick-check/'. $guideline->nid,
					array('query' => array('url' => $_GET['url'], 'mode' => 'highlight-frame'), 'attributes' => array('target' => '_blank', 'class' => 'iframe-pop-out'))) .'</p>';
		$output .= '<iframe class="preview-iframe" src="'. base_path() .'quick-check/'. $guideline->nid .'?url='. urlencode($_GET['url']) .
					'&mode=highlight-frame" title="A highlighted view of the accessibility problems with this page. You may use the link above to view this content">Your browser doesn not support frames. You may '. 
					l('open this content in a new window', 
						'quick-check/'. $guideline->nid, array('query' => array('url' => $_GET['url'], 'mode' => 'highlight-frame'))) .
					'.</iframe>';
		return $output;
	}

}

/**
*	A form callback for checking HTML content by pasting
*	it into a web form.
*/
function quail_server_quick_check_full_form() {
	$form = array();
	
	$form['html'] = array(
		'#type' => 'textarea',
		'#title' => 'Paste in HTML',
		'#required' => TRUE,
	);
	
	$form['guideline'] = array(
      '#title' => t('Guideline'),
      '#type' => 'select',
      '#options' => _quail_server_get_guidelines(),
      '#default_value' => $user->ac_server_last_quickcheck_guideline,
    );
    
    $form['submit'] = array('#type' => 'submit', '#value' => 'Check');
	
	return $form;
}

/**
*	Form submit callback to preview the accessibility of user-submitted HTML code
*	
*/
function quail_server_quick_check_full_form_submit($form, &$form_state) {
  $cms_mode = (strpos($form_state['values']['html'], '<body') === false);
  $results = quail_server_check_string($form_state['values']['html'], null, $form_state['values']['guideline'], $cms_mode);
  $form = accessible_content_accessibility_report($results, null, 'Web Page');

  return drupal_get_form($form);

}

/**
*	Adds CSS code to highlight view pages. These pages do not live within the Drupal
*	themeing system, so we have to do som fancy HTML find-replace work to put these
*	CSS links in.
*/
function _quail_server_add_css_to_highlight($html) {
  	if (module_exists('beautytips')) {
    	$options['ac_demo_view'] = array(
    		'area' => '.highlight-link',
    		'ajaxPath' => array(0 => "$(this).attr('href') + '/accessibility/tip'", 1 => '#node-tip'),
    		'trigger' => array(0 => 'mouseover', 1 => 'click'),
    		'width' => 350,
    	);
    	beautytips_add_beautytips($options);
  	}
  	global $base_path;
  	$additions = '<link type="text/css" rel="stylesheet" media="all" href="'. 
  				 $base_path . 
  				 drupal_get_path('module', 'accessible_content') .'/accessible_content.css" />';
  	$additions .= '<link type="text/css" rel="stylesheet" media="all" href="'. 
  				 $base_path . 
  				 drupal_get_path('module', 'quail_server') .'/highlight.css" />';
  	$additions .= drupal_get_js();
  	return str_replace('</head>', $additions .'</head>', $html);
}

function quail_server_websites_tests_overview($node) {
	$query = db_query('select COUNT(t.hash) as total,  t.test_nid, (SELECT title FROM node WHERE nid = t.test_nid) as title, t.hash,
t.start_text FROM quail_server_page_tests t
LEFT JOIN quail_server_webpage w ON w.vid = t.page_vid
WHERE w.website_nid = %d
GROUP BY t.hash
ORDER BY total DESC
LIMIT %d', $node->nid, 20);
	$table = array();
	$header = array('Total', 'Pages', 'Test', 'Common Problem');
	while($row = db_fetch_object($query)) {
		$table[] = array($row->total,
						l('View All Pages', 'node/'. $node->nid .'/test-overview/'. $row->test_nid .'/'. $row->hash),
					     l($row->title, 'node/'. $row->test_nid),
					     $row->start_text);
	}
	
	return theme('table', $header, $table) .
		   theme('pager', array(1, 2), 20);
}

function quail_server_websites_tests_specifics($node, $test, $hash) {
	$header = array('Page', 'URL');
	$table = array();
	$query = db_query('select w.nid, (SELECT title FROM node WHERE nid = w.nid) as title
	    FROM {quail_server_page_tests} t
		LEFT JOIN {quail_server_webpage} w ON w.vid = t.page_vid
		WHERE w.website_nid = %d
		AND t.hash = "%s"
		AND t.test_nid = %d
		ORDER BY title
		LIMIT %d', $node->nid, $hash, $test, 20);
	while($row = db_fetch_object($query)) {
		$page = node_load($row->nid);
		$table[] = array(l($page->title, 'node/'. $page->nid),
						 l($page->original_url, $page->original_url));
	}
	$sample = db_result(db_query('SELECT start_text FROM {quail_server_page_tests} WHERE hash = "%s"
								  AND test_nid = %d',
								  $hash,
								  $test));
	return '<div><h4>Pages with an error that includes: </h4>'. $sample .'</div>'. theme('table', $header, $table);
}

function quail_server_highlight_code_page($node) {
	  module_load_include('inc', 'quail_server');
	  drupal_add_css(drupal_get_path('module', 'quail_server') .'/quail_server.css');
	  global $base_url;
	  $website = node_load($node->website);
      $htmlclass = new quailServerRenderHTML();

	  accessible_content_include_library();
	  $result = drupal_http_request($node->original_url);
	  $display_level = array(
      	QUAIL_TEST_SEVERE  => TRUE,
      	QUAIL_TEST_MODERATE => TRUE,
      	QUAIL_TEST_SUGGESTION => TRUE,
	  );
	  if(!$result) {
	  	return '<h2>Could not access address.</h2>';
	  }
	  
	  $text = _quail_server_get_content_from_template($result->data, $website, $node->original_url, FALSE);
  	  $cms_mode = ($text != $result->data);
	  $text = str_replace('&nbsp;', ' ', $text);
	  $text = $htmlclass->completeURLs($text, $node->original_url);
       if(module_exists('htmlLawed')) {
       	 $text = _htmLawed_process($text);
       }
	  $quail = new quail($text, 'accessible_content', 'string', 'codeHighlight');
	  $quail->setUri($node->original_url);
	  $quail->setOption('cms_mode', $cms_mode);
	  if($test_nid) {
	    $test = node_load($test_nid);
	    $quail->runCheck(array('ac_module_test' => $test, 'display_level' => $display_level));
	    
	  }
	  else {
	  	$website = node_load($node->website);
	    $guideline = node_load($website->ac_server['guideline_nid']);
	    $quail->runCheck(array('ac_module_guideline' => $guideline, 'display_level' => $display_level));
	  }
	  $html = $quail->getReport(array('image_url' => array(
          QUAIL_TEST_SEVERE => $base_url .'/'.
            variable_get('ac_ui_icon_severe',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-severe.png'),
          QUAIL_TEST_MODERATE => $base_url .'/'.
            variable_get('ac_ui_icon_moderate',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-moderate.png'),
          QUAIL_TEST_SUGGESTION => $base_url .'/'.
            variable_get('ac_ui_icon_suggestion',
              drupal_get_path('module', 'accessible_content') .'/images/test-demo-suggestion.png'),
              ),
              'highlight_classes' => array(
                QUAIL_TEST_SEVERE => variable_get('ac_ui_class_severe', 'accessibility-error-severe'),
                QUAIL_TEST_MODERATE => variable_get('ac_ui_class_moderate', 'accessibility-error-moderate'),
                QUAIL_TEST_SUGGESTION => variable_get('ac_ui_class_severe', 'accessibility-error-suggestion'),
              ),
       ));

       return $html;
}